
/*
 * DoghostView.java
 */

package doghost;

import doghost.reports.ContasReceberPeriodo;
import doghost.selectors.AnimalSelector;
import doghost.selectors.CidadeSelector;
import doghost.selectors.ClienteSelector;
import doghost.selectors.DiariaSelector;
import doghost.selectors.HospedagemSelector;
import doghost.selectors.RacaSelector;
import doghost.selectors.RacaoSelector;
import doghost.selectors.ServicoSelector;
import doghost.selectors.UfSelector;
import doghost.selectors.VeterinarioSelector;
import doghost.utils.ThemeChooser;
import java.awt.Color;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UnsupportedLookAndFeelException;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.UIManager;

/**
 * The application's main frame.
 */
public class DoghostView extends FrameView {

    public DoghostView(SingleFrameApplication app) throws UnsupportedLookAndFeelException {
        super(app);

        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        }

        UIManager.put("TaskPane.background",Color.white);

        initComponents();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            @Override
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = DoghostApp.getApplication().getMainFrame();
            aboutBox = new DoghostAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        DoghostApp.getApplication().show(aboutBox);
    }

    @Action
    public void selectUf() {
        UfSelector ufSelector = new UfSelector(DoghostApp.getApplication().getMainFrame(), true);
        ufSelector.setLocationRelativeTo(DoghostApp.getApplication().getMainFrame());
        ufSelector.setVisible(true);
    }

    @Action
    public void selectCidade() {
        CidadeSelector cidadeSelector = new CidadeSelector(DoghostApp.getApplication().getMainFrame(), true);
        cidadeSelector.setLocationRelativeTo(DoghostApp.getApplication().getMainFrame());
        cidadeSelector.setVisible(true);
    }

    @Action
    public void selectCliente() {
        ClienteSelector clienteSelector = new ClienteSelector(DoghostApp.getApplication().getMainFrame(), true);
        clienteSelector.setLocationRelativeTo(DoghostApp.getApplication().getMainFrame());
        clienteSelector.setVisible(true);
    }

    @Action
    public void selectServico() {
        ServicoSelector servicoSelector = new ServicoSelector(DoghostApp.getApplication().getMainFrame(), true);
        servicoSelector.setLocationRelativeTo(DoghostApp.getApplication().getMainFrame());
        servicoSelector.setVisible(true);
    }

    @Action
    public void selectHospedagem() {
        HospedagemSelector hospedagemSelector = new HospedagemSelector(DoghostApp.getApplication().getMainFrame(), true);
        hospedagemSelector.setLocationRelativeTo(DoghostApp.getApplication().getMainFrame());
        hospedagemSelector.setVisible(true);
    }

    @Action
    public void selectAnimal() {
        AnimalSelector animalSelector = new AnimalSelector(DoghostApp.getApplication().getMainFrame(), true);
        animalSelector.setLocationRelativeTo(DoghostApp.getApplication().getMainFrame());
        animalSelector.setVisible(true);
    }

    @Action
    public void selectVeterinario() {
        VeterinarioSelector veterinarioSelector = new VeterinarioSelector(DoghostApp.getApplication().getMainFrame(), true);
        veterinarioSelector.setLocationRelativeTo(DoghostApp.getApplication().getMainFrame());
        veterinarioSelector.setVisible(true);
    }

    @Action
    public void selectRaca() {
        RacaSelector racaSelector = new RacaSelector(DoghostApp.getApplication().getMainFrame(), true);
        racaSelector.setLocationRelativeTo(DoghostApp.getApplication().getMainFrame());
        racaSelector.setVisible(true);
    }

    @Action
    public void selectRacao() {
        RacaoSelector racaoSelector = new RacaoSelector(DoghostApp.getApplication().getMainFrame(), true);
        racaoSelector.setLocationRelativeTo(DoghostApp.getApplication().getMainFrame());
        racaoSelector.setVisible(true);
    }

    @Action
    public void reportContasReceberPeriodo() {
        try {
            new ContasReceberPeriodo();
        } catch (ParseException ex) {
            Logger.getLogger(DoghostView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Action
    public void reportContasRecebidasPeriodo() {
        try {
            new ContasReceberPeriodo(new Boolean(true));
        } catch (ParseException ex) {
            Logger.getLogger(DoghostView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Action
    public void selectDiaria() {
        DiariaSelector diariaSelector = new DiariaSelector(DoghostApp.getApplication().getMainFrame(), true);
        diariaSelector.setLocationRelativeTo(DoghostApp.getApplication().getMainFrame());
        diariaSelector.setVisible(true);
    }

    @Action
    public void updateTheme() {
        ThemeChooser themeChooser = new ThemeChooser(DoghostApp.getApplication().getMainFrame(), true);
        themeChooser.setLocationRelativeTo(DoghostApp.getApplication().getMainFrame());
        themeChooser.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new org.jdesktop.swingx.JXPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        ufMenuItem = new javax.swing.JMenuItem();
        cidadeMenuItem = new javax.swing.JMenuItem();
        clienteMenuItem = new javax.swing.JMenuItem();
        animalMenuItem = new javax.swing.JMenuItem();
        servicoMenuItem = new javax.swing.JMenuItem();
        veterinarioMenuItem = new javax.swing.JMenuItem();
        racaMenuItem = new javax.swing.JMenuItem();
        racaoMenuItem = new javax.swing.JMenuItem();
        diariaMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        hospedagemMenu = new javax.swing.JMenu();
        hospedagemMenuItem = new javax.swing.JMenuItem();
        reportMenu = new javax.swing.JMenu();
        contasReceberMenuItem = new javax.swing.JMenuItem();
        contasRecebidasMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        themeChooserMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new org.jdesktop.swingx.JXPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        doghostPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("doghostPU").createEntityManager();

        mainPanel.setName("mainPanel"); // NOI18N

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(doghost.DoghostApp.class).getContext().getResourceMap(DoghostView.class);
        jLabel1.setIcon(resourceMap.getIcon("jLabel1.icon")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(51, 51, 51)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(106, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addContainerGap(59, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addGap(51, 51, 51))
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(doghost.DoghostApp.class).getContext().getActionMap(DoghostView.class, this);
        ufMenuItem.setAction(actionMap.get("selectUf")); // NOI18N
        ufMenuItem.setText(resourceMap.getString("ufMenuItem.text")); // NOI18N
        ufMenuItem.setName("ufMenuItem"); // NOI18N
        fileMenu.add(ufMenuItem);

        cidadeMenuItem.setAction(actionMap.get("selectCidade")); // NOI18N
        cidadeMenuItem.setText(resourceMap.getString("cidadeMenuItem.text")); // NOI18N
        cidadeMenuItem.setName("cidadeMenuItem"); // NOI18N
        fileMenu.add(cidadeMenuItem);

        clienteMenuItem.setAction(actionMap.get("selectCliente")); // NOI18N
        clienteMenuItem.setText(resourceMap.getString("clienteMenuItem.text")); // NOI18N
        clienteMenuItem.setName("clienteMenuItem"); // NOI18N
        fileMenu.add(clienteMenuItem);

        animalMenuItem.setAction(actionMap.get("selectAnimal")); // NOI18N
        animalMenuItem.setText(resourceMap.getString("animalMenuItem.text")); // NOI18N
        animalMenuItem.setName("animalMenuItem"); // NOI18N
        fileMenu.add(animalMenuItem);

        servicoMenuItem.setAction(actionMap.get("selectServico")); // NOI18N
        servicoMenuItem.setText(resourceMap.getString("servicoMenuItem.text")); // NOI18N
        servicoMenuItem.setName("servicoMenuItem"); // NOI18N
        fileMenu.add(servicoMenuItem);

        veterinarioMenuItem.setAction(actionMap.get("selectVeterinario")); // NOI18N
        veterinarioMenuItem.setText(resourceMap.getString("veterinarioMenuItem.text")); // NOI18N
        veterinarioMenuItem.setName("veterinarioMenuItem"); // NOI18N
        fileMenu.add(veterinarioMenuItem);

        racaMenuItem.setAction(actionMap.get("selectRaca")); // NOI18N
        racaMenuItem.setText(resourceMap.getString("racaMenuItem.text")); // NOI18N
        racaMenuItem.setName("racaMenuItem"); // NOI18N
        fileMenu.add(racaMenuItem);

        racaoMenuItem.setAction(actionMap.get("selectRacao")); // NOI18N
        racaoMenuItem.setText(resourceMap.getString("racaoMenuItem.text")); // NOI18N
        racaoMenuItem.setName("racaoMenuItem"); // NOI18N
        fileMenu.add(racaoMenuItem);

        diariaMenuItem.setAction(actionMap.get("selectDiaria")); // NOI18N
        diariaMenuItem.setText(resourceMap.getString("diariaMenuItem.text")); // NOI18N
        diariaMenuItem.setName("diariaMenuItem"); // NOI18N
        fileMenu.add(diariaMenuItem);

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setText(resourceMap.getString("exitMenuItem.text")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        hospedagemMenu.setText(resourceMap.getString("hospedagemMenu.text")); // NOI18N
        hospedagemMenu.setName("hospedagemMenu"); // NOI18N

        hospedagemMenuItem.setAction(actionMap.get("selectHospedagem")); // NOI18N
        hospedagemMenuItem.setText(resourceMap.getString("hospedagemMenuItem.text")); // NOI18N
        hospedagemMenuItem.setName("hospedagemMenuItem"); // NOI18N
        hospedagemMenu.add(hospedagemMenuItem);

        menuBar.add(hospedagemMenu);

        reportMenu.setText(resourceMap.getString("reportMenu.text")); // NOI18N
        reportMenu.setName("reportMenu"); // NOI18N

        contasReceberMenuItem.setAction(actionMap.get("reportContasReceberPeriodo")); // NOI18N
        contasReceberMenuItem.setText(resourceMap.getString("contasReceberMenuItem.text")); // NOI18N
        contasReceberMenuItem.setName("contasReceberMenuItem"); // NOI18N
        reportMenu.add(contasReceberMenuItem);

        contasRecebidasMenuItem.setAction(actionMap.get("reportContasRecebidasPeriodo")); // NOI18N
        contasRecebidasMenuItem.setText(resourceMap.getString("contasRecebidasMenuItem.text")); // NOI18N
        contasRecebidasMenuItem.setName("contasRecebidasMenuItem"); // NOI18N
        reportMenu.add(contasRecebidasMenuItem);

        menuBar.add(reportMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        themeChooserMenuItem.setAction(actionMap.get("updateTheme")); // NOI18N
        themeChooserMenuItem.setText(resourceMap.getString("themeChooserMenuItem.text")); // NOI18N
        themeChooserMenuItem.setName("themeChooserMenuItem"); // NOI18N
        helpMenu.add(themeChooserMenuItem);

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setText(resourceMap.getString("aboutMenuItem.text")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 216, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem animalMenuItem;
    private javax.swing.JMenuItem cidadeMenuItem;
    private javax.swing.JMenuItem clienteMenuItem;
    private javax.swing.JMenuItem contasReceberMenuItem;
    private javax.swing.JMenuItem contasRecebidasMenuItem;
    private javax.swing.JMenuItem diariaMenuItem;
    private javax.persistence.EntityManager doghostPUEntityManager;
    private javax.swing.JMenu hospedagemMenu;
    private javax.swing.JMenuItem hospedagemMenuItem;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private org.jdesktop.swingx.JXPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JMenuItem racaMenuItem;
    private javax.swing.JMenuItem racaoMenuItem;
    private javax.swing.JMenu reportMenu;
    private javax.swing.JMenuItem servicoMenuItem;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private org.jdesktop.swingx.JXPanel statusPanel;
    private javax.swing.JMenuItem themeChooserMenuItem;
    private javax.swing.JMenuItem ufMenuItem;
    private javax.swing.JMenuItem veterinarioMenuItem;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
}
